//
//  CreateJobOwnerInfoView.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/3/7.
//

import UIKit
import SnapKit
class CreateJobReviewOwnerInfoView: View {
    let titleView = CreateJobReviewSectionView(R.string.localizable.ownerDetails().uppercased(),haveEditBtnFlag: true)
    lazy var individualOwnerView = CreateJobReviewIndividualOwnerInfoView()
    lazy var coownerView = CreateJobReviewCoOwnerView()
    lazy var comertialOwnerView = CreateJobReviewCommertialOwnerInfoView()
    
    override func setUpView() {
        //titleView
        self.addSubview(titleView)
        titleView.snp.makeConstraints { make in
            make.top.left.right.equalToSuperview()
            make.height.greaterThanOrEqualTo(0)
        }
    }
    func setData(){
        if CreateJobModelHelper.shared.createJobViewModel.propertyOwner.owner_type == .individual{
            self.individualOwnerView.setData()
            //先删除commertial view
            if let _ = self.comertialOwnerView.superview{
                self.comertialOwnerView.removeFromSuperview()
            }
            if CreateJobModelHelper.shared.createJobViewModel.propertyOwner.co_owner_optional {
                //individual
                self.addSubview(individualOwnerView)
                individualOwnerView.snp.makeConstraints { make in
                    make.top.equalTo(titleView.snp.bottom)
                    make.height.greaterThanOrEqualTo(0)
                    make.left.right.equalToSuperview()
                }
                //coowner view
                self.addSubview(coownerView)
                coownerView.snp.makeConstraints { make in
                    make.top.equalTo(individualOwnerView.snp.bottom)
                    make.height.greaterThanOrEqualTo(0)
                    make.left.right.equalToSuperview()
                    make.bottom.equalToSuperview()
                }
                self.coownerView.setData()
            }else{
                //没有coowner view
                if let _ = self.coownerView.superview{
                    self.coownerView.removeFromSuperview()
                }
                self.addSubview(individualOwnerView)
                individualOwnerView.snp.makeConstraints { make in
                    make.top.equalTo(titleView.snp.bottom)
                    make.height.greaterThanOrEqualTo(0)
                    make.left.right.bottom.equalToSuperview()
                }
            }
            
        }else{
            //先删除individual view
            if let _ = self.individualOwnerView.superview{
                self.individualOwnerView.removeFromSuperview()
            }
            if let _ = self.coownerView.superview{
                self.coownerView.removeFromSuperview()
            }
            self.addSubview(comertialOwnerView)
            comertialOwnerView.snp.makeConstraints { make in
                make.top.equalTo(titleView.snp.bottom)
                make.height.greaterThanOrEqualTo(0)
                make.left.right.bottom.equalToSuperview()
            }
            self.comertialOwnerView.setData()
        }
    }
}
